// grid.h

#ifndef GRID_H
#define GRID_H

#include <d3d9.h>
#include <d3dx9math.h>
#include <d3dx9tex.h>
#include <d3dx9mesh.h>

#ifndef PI
#define PI 3.14159265f
#endif

struct GRIDVERTEX
{
	D3DXVECTOR3 p;  // The untransformed, 3D position for the vertex
	D3DXVECTOR3 n;  // normalized normal
	DWORD color;    // color for this vertex
	float u, v;		// texture coordinates
};

#define D3DFVF_GRIDVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX1)


class Grid {
   private:
	  int                        maxWidthVertices;    // width in vertices
	  int                        maxHeightVertices;   // height in vertices
	  int                        widthVertices;    // width in vertices
	  int                        heightVertices;   // height in vertices
	  int                        widthFaces;       // widht in faces
	  int                        heightFaces;      // height in faces
	  unsigned long              maxNumVertices;
	  unsigned long              numVertices;
	  int                        numFaces;
	  D3DXVECTOR3                v0, v1, v2, v3;   // in D3D face order
	  D3DXVECTOR3                dv;               // delta v from 0 to 2
	  GRIDVERTEX                 *vertices;
	  WORD                       *indexData;
	  LPD3DXMESH              mesh;  

   public:
	  Grid(
		 int                  maxWidthVerticesP,
		 int                  maxHeightVerticesP,
		 D3DXVECTOR3            *v0P,
		 D3DXVECTOR3            *v1P,
		 D3DXVECTOR3            *v2P,
		 D3DXVECTOR3            *v3P
	  );


	  ~Grid() {
		 delete vertices;
		 delete indexData;
	  };


	  void Update(
	  );


	  void GetGridPoint(
		 D3DXVECTOR3   *pt,
		 float    x,
		 float    z
	  );

	  void GetGridPoint(
		 D3DXVECTOR3   *pt,
		 int         i,          // row (from bottom to top)
		 int         j           // column (from left to right)
	  );

	  void GetGridNormal(
		 D3DXVECTOR3   *n,
		 D3DXVECTOR3   *pt
	  );

	  void GetGridNormal(
		 D3DXVECTOR3   *n,
		 int         i,          // row (from bottom to top)
		 int         j           // column (from left to right)
	  );


	  void CreateDirectXMesh(
		 LPDIRECT3DDEVICE9       pd3dDevice
	  );

	  void DrawDirectXMesh(
		 LPDIRECT3DDEVICE9       pd3dDevice
	  );


};

#endif

